import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import { AddressSelector } from 'antd-pro-biz';
import { Icon } from 'antd';
import api from '@/api';
import { TRANSPORT_TYPE } from '@/Constants';
import _ from 'lodash';

@connect(({ }) => ({
}))
class AddressNew extends Component {
  constructor(props) {
    super(props);
    this.state = {
      groups: [],
      selectedItems: [],
      countryArr:[],
    };
  }

  componentDidMount() {
    // this.initGroups();

    const { dispatch } = this.props
    dispatch({
      type:'cooperativeStore/findAllCountry',
      payload: {
        data: {
          
        }
      }
    }).then(res=>{
      console.log("res返回所有国家数组列表:",res)
      
      if(res.code==200){
        this.setState({
          countryArr:res.data.list
        })
      }
    })
  }



  // async initGroups() {
  //   let { groups } = this.state;
  //   // const { addressType, maxLevel } = this.props;
    
  //   let resGroup = await this.handleSearchGroup()
  //   console.log("resGroup------:",resGroup)
  //   if (resGroup.errorCode === 0) {
  //     groups = resGroup.data.map(g => {
  //       // 0表示国内，1表示港澳台，2表示国际
  //       if (g.code == '0') {
  //         // maxLevel ? g.maxLevel = maxLevel : addressType == TRANSPORT_TYPE.AIR ? g.maxLevel = 3 : g.maxLevel = 4;
  //         maxLevel=0;
  //       } else if (g.code == '1') {
  //         g.maxLevel = 3;
  //       } else {
  //         g.maxLevel = 2;

  //       }
  //       return g;
  //     });
  //   }
  //   this.setState({ groups });
  // }

  // 获取机场或地址组别
  handleSearchGroup = params => {
    // const { addressType } = params;
    if (2) {
      // return api.geolocation.portsGroup();
    } else {
      // return api.geolocation.addressGroup();
    }
    
    // if (addressType === TRANSPORT_TYPE.AIR) {
    //   return api.cooperativeStore.findAllCountry();// 获取机场地址组别(内地,港澳台,国际)
    // } else {
    //   return api.cooperativeStore.selectByParentCode();// 获取所有地址组别(内地,港澳台,国际)
    // }
  };

  handleSearch = params => {
    const { addressType } = this.props;
    params.addressType = addressType;
    params.pageSize = 10;
    if (addressType === TRANSPORT_TYPE.AIR) {
      return api.geolocation.portsSearch({ params });
    } else {
      return api.geolocation.addressSearch({ params });
    }
  };


  handleChange = selectedItems => {
    const { onChange } = this.props;
    if (onChange) {
      onChange(selectedItems);
    }
  };

  renderInputValue = (items) => {
    const { type, addressType, isProduct } = this.props;
    if (_.isEmpty(items)) return '';
    let lastItem = items[items.length - 1];
    let itemsNames = items.map(n => n.name).join('-');
    let lastItemNames = lastItem.name;
    if (lastItem.groupCode == 0) {
      if (addressType == TRANSPORT_TYPE.AIR && lastItem.level == 3) {
        return lastItemNames;
      } else {
        return itemsNames;
      }
    } else {
      if (addressType == TRANSPORT_TYPE.AIR && lastItem.level == 2) {
        return lastItemNames;
      } else {
        return itemsNames;
      }
    }
  };

  render() {
    const { groups } = this.state;
    const { type, value, placeholder, addonAfterHide, addonAfter, selectorStyle, contentStyle, disabled } = this.props;
    return (
      <AddressSelector
        type={type}
        value={value}
        topTabData={groups}
        onSearch={this.handleSearch}
        colSpan={8}
        inputProps={{
          disabled: disabled,
          placeholder: placeholder || '请选择地址',
          style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', background: 'transparent' },
          addonAfter: !addonAfterHide && (addonAfter || <Icon type="ellipsis" />),
          renderValue: this.renderInputValue,
        }}
        onChange={this.handleChange}
        contentStyle={contentStyle}
        style={{ width: '100%', ...selectorStyle }}
        hint="温馨提示：支持中文、拼音或首字母，如：西安 或 XA"
      />
    );
  }
}

export default AddressNew;
